package yu.zhou.njupt.fileprocessor;

import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class Main {
	
	public final static Logger logger = LoggerFactory.getLogger(Main.class);
	public final static Properties CONFIGURE = new Properties();
	
	public static void main(String[] args) {
	    try {
	        InputStream configFileInStream = Main.class.getResourceAsStream("/config.properties");
	        CONFIGURE.load(configFileInStream);
	        String sourthPathStr = CONFIGURE.getProperty("sourcePath");
	        // 遍历根目录下的所有文件，将文件的全路径名输出到指定文件
	        logger.info("开始遍历 " + sourthPathStr + " 目录下的文件,请稍候...");
	        new FileList().work("D:\\work1\\供求真实数据\\image_0928", "D:\\work1\\供求真实数据\\imagelist.txt");
	    } catch (Exception e) {
	        e.printStackTrace();
	    } finally {
	    }
	}
	
/*	public static void main(String[] args) {
	    try {
	        InputStream configFileInStream = Main.class.getResourceAsStream("/config.properties");
	        CONFIGURE.load(configFileInStream);
	        String sourthPathStr = CONFIGURE.getProperty("sourcePath");
	        String targetPathStr = CONFIGURE.getProperty("targetPath");
	        // 重命名图片
	        logger.info("开始重命名 " + sourthPathStr + " 目录下的文件,请稍候...");
	        int numOfRenamedFiles = new FileCopy2().work(sourthPathStr, targetPathStr, null, MatchMode.EQUALS, CaseSensitive.FALSE);
	        logger.info("图片重命名完毕.重命名的文件数:" + numOfRenamedFiles);
	        logger.info("重命名后的图片在" + targetPathStr); 
	    } catch (IOException e) {
	        logger.error("未找到配置文件!!!", e);
	    } catch (ClassNotFoundException e) {
	        logger.error("数据库驱动类名称步正确!!!", e);
	    } catch (SQLException e) {
	        logger.error("连接数据源失败!!!", e);
	    } catch (Exception e) {
	        logger.error("处理文件过程中出现异常!!!", e);
	    } finally {
	    }
	}*/
	
    /*public static void main(String[] args) {
    	Connection conn = null;
    	Statement st = null;
    	ResultSet rs = null;
    	
    	try {
    	    // 加载配置文件
    		InputStream configFileInStream = Main.class.getResourceAsStream("/config.properties");
    		CONFIGURE.load(configFileInStream);

    		// 连接数据源 
    		Class.forName(CONFIGURE.getProperty("target.driverClass"));
    		conn = DriverManager.getConnection(CONFIGURE.getProperty("target.url"), CONFIGURE.getProperty("target.username"), CONFIGURE.getProperty("target.password"));
    		logger.info("连接数据源成功.");
    		
    		st = conn.createStatement();
    		rs = st.executeQuery("SELECT * FROM FILE_AI_MORE WHERE OPERATE = '删除'");
    		List<String> deleteFilenameList1 = new ArrayList<String>();
    		List<String> deleteFilenameList2 = new ArrayList<String>();
    		Map<String, String> renameFilenameMap = new HashMap<String, String>();
    		while (rs.next()) {
    		    deleteFilenameList1.add(rs.getString(2));
    		    deleteFilenameList2.add(rs.getString(1));
    		    renameFilenameMap.put(rs.getString(1), rs.getString(2));
			}
    		
    		// 删除旧图片
            String sourthPathStr = "D:/sbomepcftproot/liugongsbomfile/legend/swf";
            String RenamePathStr = "D:/swf重命名后图片.09.19/";
            String bakPathStr = "D:/swf图片调整备份.09.19";
    		logger.info("开始删除" + sourthPathStr + "目录下的旧图片,请稍候...");
    		int numOfDeletedFiles = new FileDelete().work(sourthPathStr, bakPathStr, deleteFilenameList1, MatchMode.EQUALS, CaseSensitive.FALSE);
    		logger.info("旧图片删除完毕.删除的文件数:" + numOfDeletedFiles); 
    		logger.info("被删除的图片备份在" + bakPathStr);
    		
    		// 重命名图片
            logger.info("开始重命名" + sourthPathStr + "目录下的名称包含'-'的图片,请稍候...");
            int numOfRenamedFiles = new FileCopy().work(sourthPathStr, RenamePathStr, renameFilenameMap, MatchMode.EQUALS, CaseSensitive.FALSE);
            new FileDelete().work(sourthPathStr, bakPathStr, deleteFilenameList2, MatchMode.EQUALS, CaseSensitive.FALSE);
            logger.info("图片重命名完毕.重命名的文件数:" + numOfRenamedFiles);
            logger.info("重命名后的图片在" + numOfRenamedFiles); 
            logger.info("被重命名的图片备份在" + bakPathStr);
		} catch (IOException e) {
			logger.error("未找到配置文件!!!", e);
		} catch (ClassNotFoundException e) {
			logger.error("数据库驱动类名称步正确!!!", e);
		} catch (SQLException e) {
			logger.error("连接数据源失败!!!", e);
		} catch (Exception e) {
			logger.error("处理文件过程中出现异常!!!", e);
		} finally {
			DbUtils.closeQuietly(rs);
			DbUtils.closeQuietly(st);
			DbUtils.closeQuietly(conn);
		}
    }
    
/*    public static void main(String[] args) {
    	Connection conn = null;
    	Statement st = null;
    	ResultSet rs = null;
    	
    	try {
    		InputStream configFileInStream = Main.class.getResourceAsStream("/config.properties");
    		CONFIGURE.load(configFileInStream);
    		
    		 连接数据源 
    		Class.forName(CONFIGURE.getProperty("target.driverClass"));
    		conn = DriverManager.getConnection(CONFIGURE.getProperty("target.url"), CONFIGURE.getProperty("target.username"), CONFIGURE.getProperty("target.password"));
    		logger.info("连接数据源成功.");
    		
    		st = conn.createStatement();
    		rs = st.executeQuery("SELECT PART_NO FROM ZY_PART_RECAL");
    		List<String> matchStrList = new ArrayList<String>();
    		while (rs.next()) {
    			matchStrList.add(rs.getString(1));
    		}
    		
    		int numOfDeletedFiles = new Worker().work(new File("D:/sbomepcftproot/liugongsbomfile/legend"), matchStrList, 1);
    		logger.info("文件删除完毕.共删除文件数:" + numOfDeletedFiles); 
    	} catch (IOException e) {
    		logger.error("未找到配置文件!!!", e);
    	} catch (ClassNotFoundException e) {
    		logger.error("数据库驱动类名称步正确!!!", e);
    	} catch (SQLException e) {
    		logger.error("连接数据源失败!!!", e);
    	} catch (Exception e) {
    		logger.error("删除文件过程中出现异常!!!", e);
    	} finally {
    		DbUtils.closeQuietly(rs);
    		DbUtils.closeQuietly(st);
    		DbUtils.closeQuietly(conn);
    	}
    	
    }
*/    
}